package com.edevp.modules.user.factory;

import com.edevp.common.core.dto.R;
import com.edevp.modules.user.dto.UserInfoDTO;
import com.edevp.modules.user.feign.UserFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @author edevp
 */
@Component
@Slf4j
public class UserFeignClientFactory implements FallbackFactory<UserFeignClient> {

    @Override
    public UserFeignClient create(Throwable throwable) {
        // 创建UserClient接口实现类，实现其中的方法，编写失败降级的处理逻辑
        return new UserFeignClient() {
            @Override
            public R<UserInfoDTO> getUserInfo(String id) {
                // 记录异常信息
                log.error("查询用户失败", throwable);
                // 根据业务需求返回默认的数据，这里是空用户
                UserInfoDTO defaultUser = new UserInfoDTO();
                defaultUser.setName("default");
                return R.ok(defaultUser);
            }
        };
    }

    /*@Override
    public UserFeignClient create(Throwable cause) {
        UserFeignFallback fallback = new UserFeignFallback();
        fallback.setCause(cause);
        return fallback;
    }*/
}
